package com.weixing.mall.provider.controller.front;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoNeedAccess;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.MemAuth;
import com.weixing.mall.core.support.auth.MemInfo;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.core.support.auth.SysInfo;
import com.weixing.mall.provider.model.dto.*;
import com.weixing.mall.provider.model.query.AppDepotAfterSaleDetilQuery;
import com.weixing.mall.provider.model.query.AppDepotAfterSaleQuery;
import com.weixing.mall.provider.model.query.ScanCodeGoodsDetilQuery;
import com.weixing.mall.provider.model.vo.AppAuditDetilVo;
import com.weixing.mall.provider.model.vo.AppDepotAfterSaleVo;
import com.weixing.mall.provider.model.vo.ScanCodeAfterSaleVo;
import com.weixing.mall.provider.model.vo.ScanCodeGoodsDetilVo;
import com.weixing.mall.provider.service.IAfterSaleService;
import com.weixing.mall.provider.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName FrontAppDepotController
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/11/7
 * @Version 1.0
 **/
@RestController
@RequestMapping("front/appDepot")
@Api(value = "前端：App仓APi", tags = "前端：App仓")
public class FrontAppDepotController extends BaseController {

    @Autowired
    private IOrderService orderService;
    @Autowired
    private IAfterSaleService afterSaleService;



    @ApiOperation("序号自增")
    @NoNeedAccess
    @GetMapping("selfIncreasing/{orderId}")
    public Result selfIncreasing( @PathVariable Long  orderId){
        Long  increment  =afterSaleService.selfIncreasing(orderId);
        return ResultUtil.success(increment);
    }
    @ApiOperation("退款/换货 新增")
    @PostMapping("save")
    public Result save(@SysInfo SysAuth auth,@RequestBody AfterSaleDto afterSaleDto){

        afterSaleService.save(afterSaleDto,auth.getUid());
        return ResultUtil.success();
    }

    @ApiOperation("扫描检查客服是否绑定")
    @GetMapping("chekBinding/{orderId}")
    public Result save(@SysInfo SysAuth auth,@PathVariable Long orderId){
        String result=afterSaleService.chekBinding(auth,orderId);
        return null==result?ResultUtil.success():ResultUtil.error(result);
    }

    @ApiOperation(value = "扫码接收详情（商品订单信息字段部分）")
    @GetMapping("scanCodeGoodsDetil")
    public Result selectScanCodeGoodsDetil(ScanCodeGoodsDetilQuery scanCodeGoodsDetilQuery ) {
        ScanCodeGoodsDetilVo scanCodeGoodsDetilVo = orderService.selectScanCodeGoodsDetails(scanCodeGoodsDetilQuery);
        if(scanCodeGoodsDetilVo == null){
            return ResultUtil.error("商品未绑定订单");
        }
        //TODO  n拿到订单的客服和扫描人员匹配  7hao
        if(scanCodeGoodsDetilVo == null){
            return ResultUtil.error("商品未绑定订单");
        }
        return ResultUtil.success(scanCodeGoodsDetilVo);
    }

    @ApiOperation(value = "扫码接收详情（售后字段部分）")
    @GetMapping("scanCodeReceiveDetil")
    public Result selectScanCodeReceiveDetil(ScanCodeGoodsDetilQuery scanCodeGoodsDetilQuery) {
        ScanCodeAfterSaleVo scanCodeAfterSaleVo = orderService.selectScanCodeReceiveDetails(scanCodeGoodsDetilQuery);
        if(scanCodeAfterSaleVo == null){
            return ResultUtil.error("商品未绑定订单");
        }
        return ResultUtil.success(scanCodeAfterSaleVo);
    }

    /**
     * 二期APP仓库第一次接受扫描后保存接口
     * @param auth
     * @param receiveOrderDto
     * @return
     */
    @ApiOperation("扫码  接受（初审）")
    @PostMapping("receive")
    public Result receive(@SysInfo SysAuth auth, @RequestBody ReceiveOrderDto receiveOrderDto){
        afterSaleService.receive(receiveOrderDto,auth.getUid());
        return ResultUtil.success();
    }
    @ApiOperation("扫码  接受（复审）-----若调用需修改接口内容")
    @PostMapping("receivecase")
    public Result receivecase(@SysInfo SysAuth auth, @RequestBody ReceiveOrderDto receiveOrderDto){
        afterSaleService.receive(receiveOrderDto,auth.getUid());
        return ResultUtil.success();
    }
    @ApiOperation("扫码  驳回（初审拒绝）")
    @PostMapping("refuse")
    public Result refuse(@SysInfo SysAuth auth,@RequestBody ReceiveOrderDto receiveOrderDto){
        afterSaleService.refuse(receiveOrderDto,auth.getUid());
        return ResultUtil.success();
    }

    @ApiOperation(value = "App仓退换货列表")
    @GetMapping("appDepotAfterSaleList")
    public Result selectAppDepotAfterSaleList(AppDepotAfterSaleQuery appDepotAfterSaleQuery) {
        IPage<AppDepotAfterSaleVo> list = afterSaleService.selectAppDepotAfterSaleList(initMpPage(),appDepotAfterSaleQuery);
        return ResultUtil.success(list);
    }

    @ApiOperation("App仓退换货详情")
    @GetMapping("appDepotAfterSaleDetil")
    public Result selectAppDepotAfterSaleDetil(AppDepotAfterSaleDetilQuery appDepotAfterSaleDetilQuery){
        return ResultUtil.success(afterSaleService.selectAppDepotAfterSaleDetil(appDepotAfterSaleDetilQuery));
    }

    /**
     * 二期APP仓库第二次扫描订单详情
     * @param scanCodeGoodsDetilQuery
     * @return
     */
    @ApiOperation(value = "审核详情")
    @GetMapping("appAuditDetil")
    public Result selectAppAuditDetil(ScanCodeGoodsDetilQuery scanCodeGoodsDetilQuery) {
        AppAuditDetilVo appAuditDetilVo = afterSaleService.selectAppAuditDetil(scanCodeGoodsDetilQuery);
        if(ObjectUtil.isEmpty(appAuditDetilVo)){
            return ResultUtil.error("商品未绑定订单");
        }
        return ResultUtil.success(appAuditDetilVo);
    }

    @ApiOperation(value = "入问题订单")
    @PostMapping("afterSaleChgProblem")
    public Result afterSaleChgProblem(@SysInfo SysAuth auth,@RequestBody @Validated AfterSaleChgProblemDto afterSaleChgProblemDto){
        afterSaleService.afterSaleChgProblem(afterSaleChgProblemDto,auth);
        return ResultUtil.success();
    }

    @ApiOperation(value = "修改售后状态")
    @PostMapping("afterSaleChgStatus")
    public Result afterSaleChgStatus(@RequestBody @Validated AfterSaleChgStatusDto afterSaleChgStatusDto){
        afterSaleService.afterSaleChgStatus(afterSaleChgStatusDto);
        return ResultUtil.success();
    }



}
